﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Control.Bus;

namespace QuanLyKho
{
    public partial class FrmBaoCaoTonKho : Form
    {
        public FrmBaoCaoTonKho()
        {
            InitializeComponent();
        }

        private void FrmBaoCaoTonKho_Load(object sender, EventArgs e)
        {
            //List<BusWareHouse> whs = BusWareHouse.GetAll();
            //var groub = whs.GroupBy(wh => wh.Date.Year).OrderBy(wh => wh.Key);
            //foreach (var item in groub)
            //{
            //    cbbNam.Items.Add(item.Key);
            //}
            dataGridView1.AutoGenerateColumns = false;
            BindingSource();
        }

        private void BindingSource()
        {
            DateTime fromDate = dateTimePicker1.Value;
            DateTime toDate = dateTimePicker2.Value;
            List<BusWareHouse> whs = new List<BusWareHouse>();
            whs = BusWareHouse.GetByRangeDate(fromDate, toDate);
            var groupwh = whs.GroupBy(wh => wh.ProdCode).OrderBy(wh=>wh.Key)
                .Select(emp => new BusWareHouse()
                {

                    Import = emp.Sum(p => p.Import),
                    Export = emp.Sum(p => p.Export),
                    //Store = emp.Sum(p => p.Import)-emp.Sum(p => p.Export),
                    Prod = BusProduct.GetProdByCode(emp.Key),
                    Date = DateTime.Now
                });
            List<BusWareHouse> datasource = groupwh.ToList<BusWareHouse>();
            foreach (BusWareHouse item in datasource)
            {
                item.Store = item.Prod.GetTonKho(toDate.AddDays(1));
            }
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = datasource;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BindingSource();
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count != 0)
            {
                DateTime fromDate = dateTimePicker1.Value;
                DateTime toDate = dateTimePicker2.Value;
                string ProdCode = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                FrmBaoCaoNhap frm = new FrmBaoCaoNhap(ProdCode, fromDate, toDate);
                frm.Show();
            }            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count != 0)
            {
                DateTime fromDate = dateTimePicker1.Value;
                DateTime toDate = dateTimePicker2.Value;
                string ProdCode = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                frmBaoCaoXuat frm = new frmBaoCaoXuat(ProdCode, fromDate, toDate);
                frm.Show();
            }
        }
    }
}
